//
// NLSESolverAppController.m
// NLSE Solver, Created by jmpp on 9/2/10.
// $Id: NLSESolverAppController.m 18 2010-11-04 16:17:41Z jmpalacios $

/**
* =================================================================
* Copyright (C) 2010, Juan Manuel Palacios. All Rights Reserved.
*
* This software project, which includes this module, is licensed
* as described in the file LICENSE, which you should have received
* as part of this distribution, and may be used only according to
* the terms described therein.
*
* =================================================================
**/


#import "NLSESolverAppController.h"


static NLSESolverAppController *appController = nil;

@implementation NLSESolverAppController

@synthesize sharedPixelFormat, sharedOpenGLContext;


+ (NLSESolverAppController *)sharedInstance
{
    
    if (!appController)
    {
        [[NLSESolverAppController alloc] init];
    }
    return appController;
    
}


- (id) init
{
    
    if (appController == nil && (self = [super init]) != nil)
    {
        NSOpenGLPixelFormatAttribute attributes[] =
        {
            NSOpenGLPFAWindow, /* Pixel formats capable of rendering to a window. */
            NSOpenGLPFAAccelerated, /* Hardware accelerated pixel format. */
            NSOpenGLPFADoubleBuffer, /* Double-buffered pixel format. */
            NSOpenGLPFAColorSize, 24, /* 24-bits color channels. */
            NSOpenGLPFAAlphaSize, 8, /* 8-bit alpha channel. */
            NSOpenGLPFADepthSize, 24, /* 24-bit depth buffer. */
            NSOpenGLPFAMinimumPolicy, /* Require a pixel format that meets or exceeds the previous requirements. */
            0
        };
        if ( (sharedPixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes]) == nil )
        {
            [self applicationShouldQuit:kOpenGLPixelFormatUnavailable];
            return nil;
        }
        if ( (sharedOpenGLContext = [[NSOpenGLContext alloc] initWithFormat:sharedPixelFormat
                                                               shareContext:nil]) == nil )
        {
            [self applicationShouldQuit:kOpenGLContextUnavailable];
            return nil;
        }
        appController = self;
    }
    return self;
    
}


- (void) applicationDidFinishLaunching:(NSNotification *) __attribute__((unused)) theNotification
{
    
    if (windowController == nil)
    {
        [self applicationShouldQuit:kApplicationWindowUnavailable];
        return;
    }
    [windowController showWindow:nil];
    return;
    
}


- (void) applicationShouldQuit:(NLSEQuitReasons)theReason
{
    
    NSString *messageText = nil;
    NSString *informativeText = nil;
    
    switch (theReason)
    {
    case kOpenGLPixelFormatUnavailable:
        messageText = NSLocalizedString(@"OPENGL_BAILOUT_MESSAGE_TEXT",@"Application bail out message due to inssuficient OpenGL hardware capabilities.");
        informativeText = NSLocalizedString(@"OPENGL_PIXELFORMAT_UNAVAILABLE",@"Alert message when no suitable OpenGL pixel format object could be created.");
        break;
    case kOpenGLContextUnavailable:
        messageText = NSLocalizedString(@"OPENGL_BAILOUT_MESSAGE_TEXT",@"Application bail out message due to inssuficient OpenGL hardware capabilities.");
        informativeText = NSLocalizedString(@"OPENGL_CONTEXT_UNAVAILABLE",@"Alert message when no OpenGL context object could be created with the supplied pixel format object.");
        break;
    case kApplicationWindowUnavailable:
        messageText = NSLocalizedString(@"GENERIC_BAILOUT_MESSAGE_TEXT",@"Application bail out message due to an irrecoverable error.");
        informativeText = NSLocalizedString(@"APPLICATION_WINDOW_UNAVAILABLE",@"Alert message when the main application window is not available for some reason.");
        break;
    }
    NSAlert *quitDialogue = [[NSAlert alloc] init];
    [quitDialogue addButtonWithTitle:NSLocalizedString(@"QUIT_BUTTON",@"Quit button label in the alert dialogue shown when certain critical, application terminating conditions arise.")];
    [quitDialogue setAlertStyle:NSCriticalAlertStyle];
    [quitDialogue setMessageText:messageText];
    [quitDialogue setInformativeText:informativeText];
    [quitDialogue runModal];
    [NSApp terminate:self];
    
    return;
    
}


@end
